package com.ecoolex.hotel.saas.core.manager;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.hotel.saas.core.dao.DepartmentDAO;
import com.ecoolex.hotel.saas.entity.Department;
import com.ecoolex.hotel.saas.entity.Department.Column;
import com.ecoolex.hotel.saas.entity.DepartmentExample;
import com.ecoolex.hotel.saas.entity.DepartmentExample.Criteria;

import java.time.Instant;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DepartmentManager {
	
    @Autowired
    private DepartmentDAO dao;
    
    @Autowired
    private DistributionManager distributionManager;
    
    @Autowired
    private SalesUserManager salesUserManager;
    
    public Department getDepartment(Integer departmentId) {
    	return dao.selectByPrimaryKeyWithLogicalDelete(departmentId, Department.NOT_DELETED);
    }
    
    public Department getDepartment(Integer departmentId, Integer storeId) {
    	DepartmentExample example = new DepartmentExample();
    	Criteria criteria = example.createCriteria();
    	if(Check.notNull(departmentId)) {
    		criteria.andDepartmentIdEqualTo(departmentId);
    	}
    	if(Check.notNull(storeId)) {
    		criteria.andStoreIdEqualTo(storeId);
    	}
    	criteria.andDeletedEqualTo(Department.NOT_DELETED);
		return dao.selectOneByExample(example);
	}

	public Pagination<Department> query(String name, Integer storeId, Page page) {
		DepartmentExample example = new DepartmentExample();
    	Criteria criteria = example.createCriteria();
    	if(Check.notNull(name)) {
    		criteria.andNameLike("%"+name+"%");
    	}
    	criteria.andStoreIdEqualTo(storeId).andDeletedEqualTo(Department.NOT_DELETED);
    	example.setOffset(page.getFirstIndex());
    	example.setRows(page.getPageSize());
    	example.orderBy(Column.createTime.desc());
		return Pagination.create(page, 
			dao.countByExample(example),
			() -> dao.selectByExample(example));
	}
	
	public List<Department> queryDepartmentByOutletsId(Integer outletsId) {
		return dao.queryDepartmentByOutletsId(outletsId);
	}

	public Department getByName(String name){
		DepartmentExample example = new DepartmentExample();
		Criteria criteria = example.createCriteria();
		criteria.andNameEqualTo(name).andDeletedEqualTo(Department.NOT_DELETED);
		return dao.selectOneByExample(example);
	}

	public Department addDepartment(Department record, String createUser){
		BizAssert.isTrue(Check.isNull(getByName(record.getName())), ResultCode.BIZ.build(10), "名称不能重复");
		record.setDeleted(Department.NOT_DELETED);
		record.setCreateUser(createUser);
		record.setCreateTime(Date.from(Instant.now()));
		record.setUpdateUser(createUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.insert(record);
		return record;
	}

	public Department getByNameNotDepartmentId(String name, Integer departmentId){
		DepartmentExample example = new DepartmentExample();
		Criteria criteria = example.createCriteria();
		criteria.andNameEqualTo(name).andDepartmentIdNotEqualTo(departmentId).andDeletedEqualTo(Department.NOT_DELETED);
		return dao.selectOneByExample(example);
	}

	public Department editDepartment(Department record, String updateUser){
		BizAssert.isTrue(Check.isNull(getByNameNotDepartmentId(record.getName(),record.getDepartmentId())), ResultCode.BIZ.build(10), "名称不能重复");
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.excludes(Column.deleted,Column.storeId,Column.createUser,Column.createTime));
		return record;
	}

	public Boolean delDepartment(Department record, String updateUser){
		record.setDeleted(Department.IS_DELETED);
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.deleted,Column.updateUser,Column.updateTime);
		//删除关联的分销配置、销售员
		distributionManager.deleteByDepartmentId(record.getDepartmentId(), updateUser);
		salesUserManager.deleteByDepartmentId(record.getDepartmentId(), updateUser);
		return true;
	}
}